package com.foreveross.crawl.common;

import java.io.Serializable;

import com.foreveross.crawl.common.domain.IBaseEntity;
import com.foreveross.crawl.common.jpa.IBaseRepository;
import com.foreveross.crawl.common.util.SpringHelper;

/**
 * 实例化工厂
 * @author xiangsf 2013-2-16
 *
 */
public final class InstanceFactory {
	
	public static <T extends IBaseEntity> T getInstance(Class<T> clazz, Serializable id) throws Exception{
		if(id == null) return getNewInstance(clazz);
		IBaseRepository baseRepository	= clazz.newInstance().getBaseEntityRepository();
		return baseRepository.find(clazz, id);
	}
	
	public static <T extends IBaseEntity> T getNewInstance(Class<T> clazz) throws Exception{
		return clazz.newInstance();
	}
	
	public static <T> T getInstance(Class<T> clazz){
		return SpringHelper.getBean(clazz);
	}
	
	public static <T> T getInstance(Class<T> clazz, String name){
		return SpringHelper.getBean(clazz, name);
	}
	
	public static Object getInstance(String name){
		return SpringHelper.getBean(name);
	}
}
